package reflect;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.lang.reflect.Field;

public class ReflectDemo8 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Teacher teacher = new Teacher();
        teacher.name="周杰伦";
        System.out.println(teacher);

        Class cls=Class.forName("reflect.Person");
        Object o=cls.newInstance();
        /*
            Field类，它也是反射对象之一
            它的每一个实例用于表示类中的某个属性
         */
        //获取Teacher类的name属性

        Field field =cls .getDeclaredField("name");
//        field.setAccessible(true);//如果是私有属性，可以强行访问
        field.set(o,"妈卖批");
//        field.setAccessible(false);
        System.out.println(o);

    }
}
